using System.Collections.Concurrent;
using System.Collections.Generic;
using PersonMgrApp.Interfaces;

namespace PersonMgrApp.Implementations
{
    public class InMemoryDAL : IDataAccessLayer
    {
        private readonly ConcurrentDictionary<string, int> _personDictionary;

        public InMemoryDAL()
        {
            _personDictionary = new ConcurrentDictionary<string, int>();

            _personDictionary["Greg"] = 45;
            _personDictionary["John"] = 46;
            _personDictionary["Adam"] = 47;
            _personDictionary["Wayne"] = 48;
            _personDictionary["Amber"] = 49;
            _personDictionary["Tanya"] = 50;
            _personDictionary["Xavier"] = 51;
        }

        public Dictionary<string, int> GetPersonAges()
        {
            return new Dictionary<string, int>(_personDictionary);
        }

        public void UpsertPerson(string name, int age)
        {
            _personDictionary.AddOrUpdate(name, age, (str, integer) => age);
        }

        public bool RemovePerson(string name)
        {
            int bitBucket;
            return _personDictionary.TryRemove(name, out bitBucket);
        }
    }
}